﻿




using System.Web.Mvc;
using Hotpot.Extensions;
using Hotpot.Infrastructure;
using Hotpot.ViewModels;

namespace Hotpot.Filters
{
    public class SiteActionFilter : IActionFilter, IExceptionFilter
    {
        private readonly HotpotContext context;
        private readonly AppSettingsHelper appSettings;

        public SiteActionFilter(HotpotContext context, AppSettingsHelper appSettings)
        {
            this.context = context;
            this.appSettings = appSettings;
        }

        #region IActionFilter Members

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            setModel(filterContext.Controller.ViewData.Model as OxiteViewModel);
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
        }

        #endregion

        #region IExceptionFilter Members

        public void OnException(ExceptionContext filterContext)
        {
            setModel(filterContext.Controller.ViewData.Model as OxiteViewModel);
        }

        #endregion

        private void setModel(OxiteViewModel model)
        {
            if (model != null)
                model.Site = new SiteViewModel(context.Site, appSettings.GetInstanceName());
        }
    }
}
